********************************************************************************
* Purpose: Analyses with SPOR (TRACES) survey variables as outcomes - both for mother and father
********************************************************************************
do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 


use "$work\final_data", clear

//Merge data with Traces survey
replace mfr_muni = Kommunekode
merge 1:1 pnr using "work\SPOR", keep(2 3) gen(_mspor)
rename mfr_yob yob

merge 1:1 pnr using "work\mfr_kids", nogen keep(3) keepusing(mfr_muni mfr_yob)

* constrain the non-merged TRACES data (merge ==2) to those in relevant year muni cells - aka cells with not a single merged person
replace _mspor = _mspor -2 
sort mfr_muni mfr_yob
by mfr_muni mfr_yob: egen help=mean(_mspor) // share in TRACES in muni*year
drop if help==0 // no one in muni*year in TRACES

gen part_m = .
replace part_m = 1 if who5_total_m != .
replace part_m = 0 if who5_total_m == . 

gen part_f = .
replace part_f = 1 if who5_total_f != .
replace part_f = 0 if who5_total_f == . 

keep if tested_avg >= 0.75 // members in analysis sample - did or did they not answer TRACES survey 

********************************************************************************
* In TRACES - response or not; Appendix Table C3
********************************************************************************
eststo clear
eststo: estpost summarize $covariates peer_treat score_e1 if part_m == 0 // atrisk
est store a
eststo: estpost summarize $covariates peer_treat score_e1 if part_m == 1
est store b

esttab a b using "$out\descriptives_spor1.tex", fragment nonum mlabels(none) label cells("mean(fmt(%13.2fc))") collabels(\multicolumn{1}{c}{Mean}) nodepvar alignment(c c) nolegend nonote replace

label var part_m "Survey response"
label var part_f "Survey response"

* m/f participates in TRACES as function of treatment in the sample we end up using
estimates clear
areg part_m peer_treat $covariates, absorb(time fe mfr_first) vce(cluster group)
testparm peer_treat 
summarize part_m if peer_treat == 0, meanonly
estimates store partm
estadd scalar means = r(mean)

areg part_f peer_treat $covariates, absorb(time fe mfr_first) vce(cluster group)
summarize part_f if peer_treat == 0, meanonly
estimates store partf
estadd scalar means = r(mean)

gen depr_m = 0
replace depr_m = 1 if who5_total_m < 50
replace depr_m = . if who5_total_m == .

gen depr_f = 0
replace depr_f = 1 if who5_total_f < 50
replace depr_f = . if who5_total_f == .

gen stim_m = .
replace stim_m = stimact_9mdr_2_m + stimact_9mdr_3_m + stimact_9mdr_4_m
replace stim_m = stimact_2aar_1_m + stimact_2aar_2_m + stimact_2aar_3_m if stim_m == .
replace stim_m = stimact_3aar_1_m + stimact_3aar_2_m + stimact_3aar_3_m if stim_m == .

gen stim_f = .
replace stim_f = stimact_9mdr_2_f + stimact_9mdr_3_f + stimact_9mdr_4_f
replace stim_f = stimact_2aar_1_f + stimact_2aar_2_f + stimact_2aar_3_f if stim_f == .
replace stim_f = stimact_3aar_1_f + stimact_3aar_2_f + stimact_3aar_3_f if stim_f == .

gen csrq_m=1
replace csrq_m=0 if csrq_1_m<4 & csrq_2_m<4
replace csrq_m=. if csrq_1_m==. & csrq_2_m==.
gen csrq_f=1
replace csrq_f=0 if csrq_1_f<4 & csrq_2_f<4
replace csrq_f=. if csrq_1_f==. & csrq_2_f==.

label var who5_total_m "WH0-5 score"
label var who5_total_f "WH0-5 score"
label var pss_total_m "PSS Mother"
label var pss_total_f "PSS Father"
label var pss_ps_m "\specialcell{Stress subscale \\ Mother}"
label var pss_ps_f "\specialcell{Stress subscale \\ Father}"
label var pss_lps_m "\specialcell{Parent satisfaction subscale \\ Mother}"
label var pss_lps_f "\specialcell{Parent satisfaction subscale \\ Father}"
label var stim_m "\specialcell{Lack of acitivities\\ Mother}"
label var stim_f "\specialcell{Lack of acitivities\\ Father}"
label var csrq_m "\specialcell{Lack of network\\ Mother}"
label var csrq_f "\specialcell{Lack of network\\ Father}"

********************************************************************************
* Table C4: Effects of exposure to a depressed peer on maternal Well-being, parental stress and behavior
********************************************************************************

preserve
keep if part_m==1

foreach v of varlist who5_total_m pss_total_m pss_ps_m pss_lps_m stim_m csrq_m{
areg `v' peer_treat $covariates, absorb(time fe mfr_first) vce(cluster group)
estimates store e`v'
summarize `v' if peer_treat == 0, meanonly
estadd scalar means = r(mean)
}
esttab partm e* using "$out\spor_m.tex", fragment b(3) se(3) keep(peer_treat) stats(means N, fmt(%13.2fc %13.0fc) label("Control group mean" "Obs.")) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace
restore

********************************************************************************
* Table C5: Effects of exposure to a depressed peer on paternal Well-being, parental stress and behavior
********************************************************************************

preserve
keep if part_f==1

foreach v of varlist who5_total_f pss_total_f pss_ps_f pss_lps_f stim_f csrq_f{
areg `v' peer_treat $covariates, absorb(time fe mfr_first) vce(cluster group)
estimates store f`v'
summarize `v' if peer_treat == 0, meanonly
estadd scalar means = r(mean)
}
esttab partf f* using "$out\spor_f.tex", fragment b(3) se(3) keep(peer_treat) stats(means N, fmt(%13.2fc %13.0fc) label("Control group mean" "Obs.")) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace	
restore
